﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

using DalatCooking.Core;

using System.Net.Mail;
using System.Text;
using System.Data;
using System.Xml;

using DalatCooking.Core.Helper;
namespace DalatCooking.Core.Web.Services
{
    /// <summary>
    /// Summary description for api
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]

    public class API : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World" + PersonManager.FindAll().Count;
        }
        [WebMethod]
        public string About()
        {
            return "Đà Lạt Xanh . Net - Email: tuanitpro@gmail.com / Mobile: 097 6060 432";
        }

        #region Thoi tiet Vnexpress
        [WebMethod]
        public string[] JsonWeather_VnExpress(string city)
        {
            string[] myData = new string[4];
            XmlTextReader reader = null;
            string url = "http://vnexpress.net/ListFile/Weather/" + city + ".xml";
            string AdImg = "";
            string AdImg1 = "";
            string AdImg2 = "";
            string Weather = "";

            reader = new XmlTextReader(url);
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(reader);

            XmlNodeList nodelist = xmldoc.SelectNodes("Item");
            XmlNode nodedetail;
            nodedetail = nodelist.Item(0).SelectSingleNode("AdImg");
            AdImg = nodedetail.InnerText;
            nodedetail = nodelist.Item(0).SelectSingleNode("AdImg1");
            AdImg1 = nodedetail.InnerText;
            nodedetail = nodelist.Item(0).SelectSingleNode("AdImg2");
            AdImg2 = nodedetail.InnerText;
            nodedetail = nodelist.Item(0).SelectSingleNode("Weather");
            Weather = nodedetail.InnerText;
            myData[0] = "<img src='/images/Weather/" + AdImg + "' border='0' width='36' height='35' />&nbsp;";
            myData[1] = AdImg1.Substring(0, 1);//"<img src='/images/Weather/" + AdImg1 + "' border='0' width='36' height='35' />&nbsp;";  // Lay so thu nhat cua nhiet do
            myData[2] = AdImg2.Substring(0, 1); //"<img src='/images/Weather/" + AdImg2 + "' border='0' width='36' height='35' />&nbsp;";  // Lay so thu 2 cua nhiet do
            myData[3] = Weather;
            return myData;
        }

        #endregion


        #region Person - Tao boi Le Thanh Tuan

        [WebMethod]
        public JsonResponse JsonPerson_Insert(string username, string password, string email)
        {
            Person p = new Person { UserName = username, Password = password, Email = email, IsActive = false };
            return JsonPerson.Insert(p);
        }
        [WebMethod]
        public JsonResponse JsonPerson_FindByEmail(string email)
        {
            return JsonPerson.FindByEmail(email);
        }
        [WebMethod(EnableSession = true)]
        public JsonResponse JsonPerson_LoginEmail(string email, string password)
        {

            string msg = "";
            bool flag = false;
            Person p = PersonManager.Login(email, password);
            if (p != null)
            {
                Session.SetPersonLogged(p);
                flag = true;
                msg = "Đăng nhập thành công.";
            }
            else
            {
                flag = false;
                msg = "Đăng nhập thất bại. Email hoặc mật khẩu không đúng.";
            }
            return new JsonResponse { Success = flag, Message = msg };
        }
        [WebMethod]
        public JsonResponse JsonPerson_FindByUserName(string username)
        {
            return JsonPerson.FindByUserName(username);
        }
        [WebMethod(EnableSession = true)]
        public JsonResponse JsonPerson_Login(string userName, string password)
        {
            string msg = "";
            bool flag = false;
            Person p = PersonManager.Login(userName, password);
            if (p != null)
            {
                Session.SetPersonLogged(p);
                flag = true;
                msg = "Đăng nhập thành công.";
            }
            else
            {
                flag = false;
                msg = "Đăng nhập thất bại. Tên đăng nhập hoặc mật khẩu không đúng.";
            }
            return new JsonResponse { Success = flag, Message = msg };
        }

        [WebMethod]
        public JsonResponse JsonPerson_AddRole(int personId, int roleId)
        {
            return JsonPerson.Person_AddRole(personId, roleId);
        }
        [WebMethod]
        public JsonResponse JsonPerson_RemoveRole(int personId, int roleId)
        {
            return JsonPerson.Person_RemoveRole(personId, roleId);
        }

        [WebMethod(EnableSession = true)]
        public JsonResponse JsonPerson_ChangePassword(string passwordOld, string passwordNew)
        {
            Person p = Session.GetPersonLogged();
            if (p == null || string.IsNullOrEmpty(passwordOld) || string.IsNullOrEmpty(passwordNew))
            {
                return new JsonResponse { Success=false, Message="Không thể thay đổi mật khẩu" };
            }
            p.Password = passwordOld.Trim();
            p.PasswordNew = passwordNew.Trim();

            return JsonPerson.Person_ChangePassword(p);
        }
        #endregion

        #region Weather Vnx

        [WebMethod]
        public string[] GetWeather(string city)
        {
            string[] myData = new string[4];
            string strWrite = "";
            XmlTextReader reader = null;
            string url = "http://vnexpress.net/ListFile/Weather/" + city + ".xml";
            string AdImg = "";
            string AdImg1 = "";
            string AdImg2 = "";
            string Weather = "";

            reader = new XmlTextReader(url);
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(reader);

            XmlNodeList nodelist = xmldoc.SelectNodes("Item");
            XmlNode nodedetail;
            nodedetail = nodelist.Item(0).SelectSingleNode("AdImg");
            AdImg = nodedetail.InnerText;
            nodedetail = nodelist.Item(0).SelectSingleNode("AdImg1");
            AdImg1 = nodedetail.InnerText;
            nodedetail = nodelist.Item(0).SelectSingleNode("AdImg2");
            AdImg2 = nodedetail.InnerText;
            nodedetail = nodelist.Item(0).SelectSingleNode("Weather");
            Weather = nodedetail.InnerText;
            //strWrite += "<img src='http://blog.meotom.net/images/Weather/" + AdImg + "' border='0' width='36' height='35' />&nbsp;";
            //strWrite += "<img src='http://blog.meotom.net/images/Weather/" + AdImg1 + "' border='0' width='19' height='28' />";
            //strWrite += "<img src='http://blog.meotom.net/images/Weather/" + AdImg2 + "' border='0' width='19' height='28' />";
            //strWrite += "<img src='http://blog.meotom.net/images/Weather/c.gif' width='35' height='28' /><br />";
            //strWrite += Weather;
            myData[0] = "<img src='/images/Weather/" + AdImg + "' border='0' width='36' height='35' />&nbsp;";
            myData[1] = AdImg1.Substring(0, 1);//"<img src='/images/Weather/" + AdImg1 + "' border='0' width='36' height='35' />&nbsp;";  // Lay so thu nhat cua nhiet do
            myData[2] = AdImg2.Substring(0, 1); //"<img src='/images/Weather/" + AdImg2 + "' border='0' width='36' height='35' />&nbsp;";  // Lay so thu 2 cua nhiet do
            myData[3] = Weather;
            return myData;
        }

        #endregion

        #region Menu / Menu Group Tao boi Le Thanh Tuan
        [WebMethod]
        public JsonResponse JsonMenuGroup_Insert(string name)
        {
            MenuGroup g = new MenuGroup { Name = name, UniqueName = name, GroupID = -1, ApplicationId = 1 };
            return JsonMenuGroup.Insert(g);
        }
        [WebMethod]
        public JsonResponse JsonMenu_SetActive(int id)
        {
            return JsonMenu.SetActive(id);
        }
        [WebMethod]
        public JsonResponse JsonMenu_SetSortIndex(int id, int isup)
        {
            return JsonMenu.SetSortIndex(id, isup);
        }
        [WebMethod]
        public JsonResponse JsonMenu_Delete(int id)
        {
            return JsonMenu.Delete(id);
        }
        #endregion

        #region Course Khoa hoc - Le Thanh Tuan

        [WebMethod]
        public PagingHelper.PagingData<Course> JsonCourse_Paging(int pageSize, int pageNumber)
        {
            int total = 0;

            //   var listData = CourseManager.FindAll().Skip(pageSize * pageNumber-1).Take(pageSize).ToList<Course>();
            var listData = JsonCourse.PagingLinq(pageSize, pageNumber, out total);
            return new PagingHelper.PagingData<Course> { ListData = listData, PageSize = pageSize, PageNumber = pageNumber, TotalRecords = total, CurrentPage = pageNumber };
        }
        [WebMethod]
        public PagingHelper.PagingData<Course> JsonCourseByCategory_Paging(int pageSize, int pageNumber, int categoryId)
        {
            var filter = CourseManager.FindAll().FindAll(c => c.CategoryId == categoryId);

            var query = from c in filter
                        select c;

            var list = query.Skip(pageSize * (pageNumber - 1)).Take(pageSize).ToList();


            return new PagingHelper.PagingData<Course> { ListData = list, PageSize = pageSize, PageNumber = pageNumber, TotalRecords = filter.Count, CurrentPage = pageNumber };
        }

        [WebMethod]
        public PagingHelper.PagingData<Category> JsonCategory_Paging(int pageSize, int pageNumber)
        {
            int total = 0;

            //   var listData = CourseManager.FindAll().Skip(pageSize * pageNumber-1).Take(pageSize).ToList<Course>();
            var listData = CategoryManager.PagingLinq(pageSize, pageNumber, out total);
            return new PagingHelper.PagingData<Category> { ListData = listData, PageSize = pageSize, PageNumber = pageNumber, TotalRecords = total, CurrentPage = pageNumber };
        }

        [WebMethod]
        public List<Course> JsonCourse_FindAll()
        {
            return CourseManager.FindAll();
        }

        [WebMethod]
        public JsonResponse JsonCourse_SetActive(int id)
        {
            return JsonCourse.SetActive(id);
        }
        [WebMethod]
        public JsonResponse JsonCourse_SetSortIndex(int id, int isup)
        {
            return JsonCourse.SetSortIndex(id, isup);
        }
        [WebMethod]
        public JsonResponse JsonCourse_Delete(int id)
        {
            return JsonCourse.Delete(id);
        }

        #endregion

        #region CourseImage Le Thanh Tuan
        [WebMethod]
        public PagingHelper.PagingData<CourseImage> JsonCourseImage_Paging(int pageSize, int pageNumber)
        {
            int total = 0;

            //   var listData = CourseManager.FindAll().Skip(pageSize * pageNumber-1).Take(pageSize).ToList<Course>();
            var listData = JsonCourseImage.PagingLinq(pageSize, pageNumber, out total);
            return new PagingHelper.PagingData<CourseImage> { ListData = listData, PageSize = pageSize, PageNumber = pageNumber, TotalRecords = total, CurrentPage = pageNumber };
        }
        [WebMethod]
        public List<CourseImage> JsonCourseImage_ByCourseID(int courseId)
        {
            return CourseImageManager.FindAll().FindAll(c => c.CourseID == courseId);
        }


        [WebMethod]
        public List<CourseImage> JsonCourseImage_FindAll()
        {
            return CourseImageManager.FindAll();
        }
        [WebMethod]
        public JsonResponse JsonCourseImage_SetActive(int id)
        {
            return JsonCourseImage.SetActive(id);
        }
        [WebMethod]
        public JsonResponse JsonCourseImage_SetSortIndex(int id, int isup)
        {
            return JsonCourseImage.SetSortIndex(id, isup);
        }
        [WebMethod]
        public JsonResponse JsonCourseImage_Delete(int id)
        {
            return JsonCourseImage.Delete(id);
        }
        #endregion

        #region Contact - Tao boi Le Thanh Tuan

        [WebMethod]
        public JsonResponse JsonContact_Insert(string name, string email, string phone, string subject, string message)
        {
            if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(email) && !string.IsNullOrEmpty(subject) && !string.IsNullOrEmpty(message))
            {
                Contact c = new Contact
                {
                    ContactID = -1,
                    FirstName = name,
                    LastName = name,
                    Address = "",
                    CompanyName = "",
                    Email = email,
                    MainPhone = phone,
                    Subject = subject,
                    Body = message
                };
                return JsonContact.Insert(c);
            }
            return new JsonResponse { Success = false, Message = "Dữ liệu không được để trống." };
        }

        [WebMethod]
        public JsonResponse JsonContact_SetIsRead(int id)
        {
            return JsonContact.SetIsRead(id);
        }
        [WebMethod]
        public JsonResponse JsonContact_Delete(int id)
        {
            return JsonContact.Delete(id);
        }

        [WebMethod]        
        public PagingHelper.PagingData<Contact> JsonContact_Paging(int pageSize, int pageNumber)
        {
            int total = 0;

            //   var listData = CourseManager.FindAll().Skip(pageSize * pageNumber-1).Take(pageSize).ToList<Course>();
            var listData = ContactManager.PagingSql(pageSize, pageNumber, out total);
            return new PagingHelper.PagingData<Contact> { ListData = listData, PageSize = pageSize, PageNumber = pageNumber, TotalRecords = total, CurrentPage = pageNumber };
        }
        #endregion

        #region Category - Nguyen Thai Hoc
        [WebMethod]
        public List<Category> JsonCategory_FindAll()
        {
            // By Le Thanh Tuan
            return CategoryManager.FindAll();
        }
        [WebMethod]
        public JsonResponse JsonCategory_SetActive(int id)
        {
            return JsonCategory.SetActive(id);
        }
        [WebMethod]
        public JsonResponse JsonCategory_SetSortIndex(int id, int isup)
        {
            return JsonCategory.SetSortIndex(id, isup);
        }
        [WebMethod]
        public JsonResponse JsonCategory_Delete(int id)
        {
            return JsonCategory.Delete(id);
        }

        #endregion


        #region Polls
        [WebMethod]
        public string Json_ShowPoll(int pID)
        {
            DataSet dsPoll = PollManager.FindPoll_ByID(pID);
            return getResultHTML(dsPoll);
        }
        [WebMethod]
        public string Json_UpdatePollCount(int pID, int cID)
        {
            //int pollID = pID;
            //int choiceID = cID;
            //update the vote count of the selected choice    

            PollManager.UpdateChoiceVote(cID);

            //save the users IP address - to block repeated vote if the BlockMode is by IP address
            string ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            PollManager.InsertPollIP(pID, ip);

            //Save the poll ID in cookie - to block repeated vote if the BlockMode is by Cookie
            HttpCookie pollCookie;// = new HttpCookie("Poll");
            string valueToStore = ""; //we store the poll IDs as comma seperated values

            //the cookie already exists and some PollIDs are already there
            if (HttpContext.Current.Request.Cookies["Poll"] != null && HttpContext.Current.Request.Cookies["Poll"]["ID"] != null)
            {
                pollCookie = HttpContext.Current.Request.Cookies["Poll"];
                valueToStore = HttpContext.Current.Request.Cookies["Poll"]["ID"] + "," + pID.ToString(); //append the current PollID to the already existing Poll IDs after a comma
            }
            else //cookie not exists - create a new one and store the ID
            {
                pollCookie = new HttpCookie("Poll");
                valueToStore = pID.ToString();
            }
            pollCookie.Values["ID"] = valueToStore;
            pollCookie.Expires = DateTime.MaxValue; //this cookie will never expire
            HttpContext.Current.Response.Cookies.Add(pollCookie);

            //get the poll result
            DataSet dsPoll = PollManager.FindPoll_ByID(pID);
            return getResultHTML(dsPoll);
        }
        private string getResultHTML(DataSet dsPoll)
        {
            int totalVotes = int.Parse(dsPoll.Tables[1].Compute("Sum(VoteCount)", String.Empty).ToString());
            System.Text.StringBuilder sbResult = new System.Text.StringBuilder();
            sbResult.Append("<ul>");
            foreach (DataRow dr in dsPoll.Tables[1].Rows)
            {
                decimal percentage = 0;
                if (totalVotes > 0)
                    percentage = decimal.Round((decimal.Parse(dr["VoteCount"].ToString()) / decimal.Parse(totalVotes.ToString())) * 100, MidpointRounding.AwayFromZero);

                string alt = dr["VoteCount"].ToString() + " votes out of " + totalVotes.ToString();
                sbResult.Append("<li>");
                sbResult.Append("<label class='poll-result'>").Append(dr["Choice"]).Append(" (").Append(dr["VoteCount"]).Append(" bình chọn - ").Append(percentage).Append("%)</label>");
                sbResult.Append("<div class='poll-chart'><img src='/images/red-bar.png' width='0%' val='").Append(percentage).Append("%' height='15px' alt='").Append(alt).Append("' title='").Append(alt).Append("' /> ").Append("</div>");
                sbResult.Append("</li>");
            }
            sbResult.Append("</ul>");
            sbResult.Append("<div class='poll-total''>Tổng cộng: ").Append(totalVotes).Append(" lượt bình chọn.</div>");
            //sbResult.Append("</p>");
            return sbResult.ToString();
        }
        #endregion



        #region News  - Le Van Oanh
        [WebMethod]
        public JsonResponse JsonNews_SetActive(int id)
        {
            return JsonNews.SetActive(id);
        }
        [WebMethod]
        public JsonResponse JsonNews_SetSortIndex(int id, int isup)
        {
            return JsonNews.SetSortIndex(id, isup);
        }
        [WebMethod]
        public JsonResponse JsonNews_Delete(int id)
        {
            return JsonNews.Delete(id);
        }

        #endregion
        #region Person Le Van Oanh
        [WebMethod]
        public List<Person> JsonPerson_FindAll()
        {
            return PersonManager.FindAll();
        }
        [WebMethod]
        public JsonResponse JsonPerson_SetActive(int id)
        {
            return JsonPerson.SetActive(id);
        }
        [WebMethod]
        public JsonResponse JsonPerson_SetSortIndex(int id, int isup)
        {
            return JsonPerson.SetSortIndex(id, isup);
        }
        [WebMethod]
        public JsonResponse JsonPerson_Delete(int id)
        {
            return JsonPerson.Delete(id);
        }

        #endregion

        #region Subject Tran Van Doan

        [WebMethod]
        public JsonResponse JsonSubject_SetActive(int id)
        {
            return JsonSubject.SetActive(id);
        }
        [WebMethod]
        public JsonResponse JsonSubject_SetSortIndex(int id, int isup)
        {
            return JsonSubject.SetSortIndex(id, isup);
        }
        [WebMethod]
        public JsonResponse JsonSubject_Delete(int id)
        {
            return JsonSubject.Delete(id);
        }
        #endregion

        #region Document Nguyen Thai Hoc

        [WebMethod]
        public JsonResponse JsonDocument_SetActive(int id)
        {
            return JsonDocument.SetActive(id);
        }
        [WebMethod]
        public JsonResponse JsonDocument_SetSortIndex(int id, int isup)
        {
            return JsonDocument.SetSortIndex(id, isup);
        }
        [WebMethod]
        public JsonResponse JsonDocument_Delete(int id)
        {
            return JsonDocument.Delete(id);
        }

        [WebMethod]
        public PagingHelper.PagingData<Document> JsonDocumentByCourse_Paging(int pageSize, int pageNumber, int courseId)
        {
            var filter = DocumentManager.FindAll().FindAll(c => c.CourseId == courseId);

            var query = from c in filter
                        select c;

            var list = query.Skip(pageSize * (pageNumber - 1)).Take(pageSize).ToList();


            return new PagingHelper.PagingData<Document> { ListData = list, PageSize = pageSize, PageNumber = pageNumber, TotalRecords = filter.Count, CurrentPage = pageNumber };
        }
        #endregion

        #region News Paging FrontPage
        [WebMethod]
        public PagingHelper.PagingData<News> JsonNews_Paging(int pageSize, int pageNumber)
        {
            int total = 0;

            //   var listData = CourseManager.FindAll().Skip(pageSize * pageNumber-1).Take(pageSize).ToList<Course>();
            var listData = NewsManager.PagingLinq(pageSize, pageNumber, out total);
            return new PagingHelper.PagingData<News> { ListData = listData, PageSize = pageSize, PageNumber = pageNumber, TotalRecords = total, CurrentPage = pageNumber };
        }
        #endregion
        #region Docuemnt Paging FrontPage
        [WebMethod]
        public PagingHelper.PagingData<Document> JsonDocument_Paging(int pageSize, int pageNumber)
        {
            int total = 0;

            //   var listData = CourseManager.FindAll().Skip(pageSize * pageNumber-1).Take(pageSize).ToList<Course>();
            var listData = DocumentManager.PagingLinq(pageSize, pageNumber, out total);
            return new PagingHelper.PagingData<Document> { ListData = listData, PageSize = pageSize, PageNumber = pageNumber, TotalRecords = total, CurrentPage = pageNumber };
        }
        #endregion
        #region Setting
        [WebMethod]
        public JsonResponse JsonSetting_Update(string name, string value)
        {
            return JsonSetting.SetValue(name, value);
        }

        [WebMethod]
        public JsonResponse JsonSetting_TestSmtp(string email, string smtpServer, string smtpServerPort, string smtpUserName,
            string smtpPassword, string sendMailOnComment, string enableSsl, string emailSubjectPrefix)
        {


            var body = new StringBuilder();
            body.Append("<div style=\"font: 11px verdana, arial\">");
            body.Append("Success");
            if (HttpContext.Current != null)
            {
                body.Append("<br /><br />______________________________________________________________<br /><br />");
                body.AppendFormat("<strong>IP address:</strong> {0}<br />", HttpContext.Current.Request.UserHostAddress);
                body.AppendFormat("<strong>User-agent:</strong> {0}", HttpContext.Current.Request.UserAgent);
            }

            body.Append("</div>");
            MailModels _mail = new MailModels();
            MailMessage m = new System.Net.Mail.MailMessage();

            m.From = new MailAddress(email); //Ex: tuanitpro@gmail.com
            m.To.Add(email);
            m.Subject = string.Format("Test mail from {0}", smtpUserName);
            m.Body = body.ToString();
            m.IsBodyHtml = true;
            _mail.MailMsg = m;
            _mail.UserName = smtpUserName; //Ex: tuantipro (username is account your email)
            _mail.Password = smtpPassword;
            _mail.UseDefaultCredentials = false;
            _mail.EnableSsl = ConvertObject.ToBool(enableSsl);
            _mail.Host = smtpServer; // smpt server for gmail
            _mail.Port = int.Parse(smtpServerPort);

            string msg = "";
            bool rs = MailServices.Send(_mail);
            if (rs)
                msg = "Gửi email thành công.";
            else msg = "Gửi email thất bại.";
            return new JsonResponse() { Success = rs, Message = string.Format(msg, "...") };
        }
        [WebMethod]
        public JsonResponse JsonSetting_SaveEmail(string email, string smtpServer, string smtpServerPort, string smtpUserName,
            string smtpPassword, string sendMailOnComment, string enableSsl, string emailSubjectPrefix)
        {

            if (!string.IsNullOrEmpty(email) && !string.IsNullOrEmpty(smtpServer) && !string.IsNullOrEmpty(smtpServerPort)
                && !string.IsNullOrEmpty(smtpUserName) && !string.IsNullOrEmpty(smtpPassword) && !string.IsNullOrEmpty(enableSsl))
            {
                Setting.SetValue("SMTP_EMAIL", email);
                Setting.SetValue("SMTP_SERVER", smtpServer);
                Setting.SetValue("SMTP_PORT", smtpServerPort);
                Setting.SetValue("SMTP_USERNAME", smtpUserName);
                Setting.SetValue("SMTP_PASSWORD", smtpPassword);
                Setting.SetValue("SMTP_SUBJECT", emailSubjectPrefix);
                return new JsonResponse() { Success = true, Message = string.Format("Lưu cấu hình email thành công.", "...") };
            }
            else
            {
                return new JsonResponse() { Success = false, Message = string.Format("Lưu cấu hình email thất bại.", "...") };
            }
        }
        #endregion

        #region Shopping Cart

        [WebMethod(EnableSession = true)]
        public JsonResponse Json_ShoppingCartAdd(int courseID)
        {
            Course c = CourseManager.FindById(courseID);
            Cart ShoppingCart;
            if (Session.GetShoppingCart() != null)
                ShoppingCart = Session.GetShoppingCart();
            else
                ShoppingCart = new Cart();

            if (c != null)
            {
                Cart cart = new Cart { CourseId = c.CourseId, Picture = c.Picture, Name = c.Name, Price = c.Price, Quantity = 1 };
                ShoppingCart.AddToCart(cart);

                Session.SetShoppingCart(ShoppingCart);
            }

            return new JsonResponse { Success = true, Message = "Thêm sản phẩm vào giỏ hàng thành công." };
        }

        [WebMethod(EnableSession = true)]
        public List<Cart> Json_ShoppingCartList()
        {
            Cart c = Session.GetShoppingCart();
            return c.ShoppingCart;
        }
        [WebMethod(EnableSession = true)]
        public JsonResponse Json_ShoppingCartUpdate(int courseID, int quantity)
        {
           
            Cart c = Session.GetShoppingCart();
            if (quantity <= 0) c.RemoveCart(courseID);
            else
                c.UpdateCart(courseID, quantity);
            return new JsonResponse { Success = true, Message = "Cập nhật số lượng sản phẩm thành công." };
        }
        [WebMethod(EnableSession = true)]
        public JsonResponse Json_ShoppingCartRemove(int courseID)
        {
            Cart c = Session.GetShoppingCart();
            c.RemoveCart(courseID);
            return new JsonResponse { Success = true, Message = "Sản phẩm đã xóa khỏi giỏ hàng thành công." };
        }

        #endregion

        #region Module Lê Thanh Tuấn

        [WebMethod]
        public JsonResponse JsonModule_SetActive(int id)
        {
            return JsonModule.SetActive(id);
        }
        [WebMethod]
        public JsonResponse JsonModule_SetSortIndex(int id, int isup)
        {
            return JsonModule.SetSortIndex(id, isup);
        }
        [WebMethod]
        public JsonResponse JsonModule_Delete(int id)
        {
            return JsonModule.Delete(id);
        }
        #endregion
    }
}
